package com.gg.fruit.controller;

import base.BaseServlet;
import com.gg.beanhelper.BeanHelper;
import com.gg.entity.Fruit;
import com.gg.fruit.service.FruitService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/AddFruitServlet")
public class AddFruitServlet extends BaseServlet {
    @Autowired
    private FruitService fruitService;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Fruit fruit=BeanHelper.getBean(request.getParameterMap(), Fruit.class);
        int end=fruitService.addFruit(fruit);
        System.out.println(fruit);
        response.setContentType("text/html;charset=utf-8");
        if (end<=0){
            response.getWriter().print("<script>alert('添加失败！！！');location.href='/fruits/admin/BSindex4.jsp'</script>");
        }else {
            response.getWriter().print("<script>alert('添加成功！！！');location.href='/fruits/GetAllFruitsServlet?pageNum=1'</script>");
        }
    }
}
